//
//  BKStatusOriginView.swift
//  微博
//
//  Created by mac on 16/8/12.
//  Copyright © 2016年 xbk. All rights reserved.
//

import UIKit
import SnapKit

//间距
let StatusTableViewCellMargin: CGFloat = 10

class BKStatusOriginView: UIView {
    
    //记录约束
    var origainViewConstraint :Constraint?
    
    var statusViewModel:BKStatusViewModel?{
    
        didSet {
        
            if let profile = statusViewModel?.status?.user?.profile_image_url{
            
                headImageView.sd_setImageWithURL(NSURL(string: profile), placeholderImage: UIImage(named: "avatar_default_big"))
            }
            screenNameLabel.text = statusViewModel?.status?.user?.screen_name
            contentLabel.text = statusViewModel?.status?.text
            scorceLabel.text = statusViewModel?.sourceContent
            
            //卸载约束
            origainViewConstraint?.uninstall()
            
            if let picUrls = statusViewModel?.status?.pic_urls where picUrls.count > 0{
            
                pictureView.hidden = false
                pictureView.picUrls = picUrls
                
                self.snp_updateConstraints(closure: { (make) -> Void in
                    
                  self.origainViewConstraint = make.bottom.equalTo(pictureView).offset(StatusTableViewCellMargin).constraint
                })
            }else{
            
                pictureView.hidden = true
                self.snp_updateConstraints(closure: { (make) -> Void in
                    
                self.origainViewConstraint = make.bottom.equalTo(contentLabel).offset(StatusTableViewCellMargin).constraint
                })
            }
        }
    }
    

    //  MARK: -- 懒加载创建控件
    //头像
    private lazy var headImageView:UIImageView = UIImageView(image: UIImage(named: "avatar_default_big"))
    //认证类型等级
    private lazy var varifiedTypeImageView: UIImageView = UIImageView(image:UIImage(named: "avatar_vip"))
    //昵称
    private lazy var screenNameLabel :UILabel = UILabel(fontSize: 15, textColor:UIColor.darkGrayColor())
    //会员等级
    private lazy var memberImageView :UIImageView = UIImageView(image: UIImage(named: "common_icon_membership"))
    //时间
    private lazy var timeLabel :UILabel = UILabel(fontSize: 15, textColor: UIColor.darkGrayColor())
    //来源
    private lazy var scorceLabel :UILabel = UILabel(fontSize: 15, textColor: UIColor.darkGrayColor())
    //微博内容
    private lazy var contentLabel :UILabel = {
    
        let label = UILabel(fontSize: 14, textColor: UIColor.darkGrayColor())
        label.numberOfLines = 0
        
        return label
    }()
    
    //原创微博的配图
    private lazy var pictureView:BKStatusPictureView = {
    
        let view = BKStatusPictureView ()
        view.backgroundColor = self.backgroundColor
        return view
    }()
    
    override init(frame: CGRect) {
        
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private func setupUI() {
    
        addSubview(headImageView)
        addSubview(varifiedTypeImageView)
        addSubview(screenNameLabel)
        addSubview(memberImageView)
        addSubview(timeLabel)
        addSubview(scorceLabel)
        addSubview(contentLabel)
        addSubview(pictureView)

        timeLabel.text = "8:00"
        
        //头像
        headImageView.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(self).offset(StatusTableViewCellMargin)
            make.leading.equalTo(self).offset(StatusTableViewCellMargin)
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
        
        //会员等级
        varifiedTypeImageView.snp_makeConstraints { (make) -> Void in
            
            make.centerX.equalTo(headImageView.snp_trailing)
            make.centerY.equalTo(headImageView.snp_bottom)
        }
        //昵称
        screenNameLabel.snp_makeConstraints { (make) -> Void in
            
            make.leading.equalTo(headImageView.snp_trailing).offset(StatusTableViewCellMargin)
            make.top.equalTo(headImageView)
        }
        //是否会员
        memberImageView.snp_makeConstraints { (make) -> Void in
            
            make.leading.equalTo(screenNameLabel.snp_trailing).offset(StatusTableViewCellMargin)
            make.top.equalTo(screenNameLabel)
        }
        //时间
        timeLabel.snp_makeConstraints { (make) -> Void in
            
            make.leading.equalTo(screenNameLabel)
            make.bottom.equalTo(headImageView)
        }
        //来源
        scorceLabel.snp_makeConstraints { (make) -> Void in
            
           make.bottom.equalTo(headImageView)
            make.leading.equalTo(timeLabel.snp_trailing).offset(StatusTableViewCellMargin)
        }
        //微博类容
        contentLabel.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(headImageView.snp_bottom).offset(StatusTableViewCellMargin)
            make.leading.equalTo(headImageView)
            make.trailing.equalTo(self).offset(-StatusTableViewCellMargin)
        }
        pictureView.snp_makeConstraints { (make) -> Void in
            
            make.top.equalTo(contentLabel.snp_bottom).offset(StatusTableViewCellMargin)
            make.leading.equalTo(contentLabel)
        }
        
        
        
        //原创微博的底部约束 = 微博内容的约束 + 间距
        self.snp_makeConstraints { (make) -> Void in
            
           self.origainViewConstraint = make.bottom.equalTo(pictureView).offset(StatusTableViewCellMargin).constraint
        }
        
    }
}
